package com.leetcode.array;

public class RotateArray {

	public static void main(String[] args) {
		int nums[] = { 1, 2 };
		int k = 3;
		rotate(nums, k);
		for (int i = 0; i < nums.length; i++) {
			System.out.print(nums[i] + ",");
		}
	}

	public static void rotate(int[] nums, int k) {
		if (k == 0) {
			return;
		}
		if (nums.length == 0 || nums.length == 1) {
			return;
		}
		if (nums.length <= k) {
			int len = nums.length;
			int j = 0;
			while (j <k) {
				int fl = nums[len - 1];
				for (int i = len - 2; i >= 0; i--) {
					nums[i + 1] = nums[i];
				}
				nums[0] = fl;
				j++;
			}
		}
		if (nums.length > k) {
			int arr[] = new int[k];
			int j = k - 1;
			for (int i = nums.length - 1; i > nums.length - 1 - k; i--) {
				arr[j] = nums[i];
				j--;
			}
			for (int i = nums.length - 1; i > 0 && i - k >= 0; i--) {
				nums[i] = nums[i - k];
			}
			for (int i = 0, n = 0; i < k; i++, n++) {
				nums[i] = arr[n];

			}
		}
	}
}
